package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.MaintainFeeAttachBiz;
import com.egao.orderB.entity.MaintainFeeAttach;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2020-06-15 15:18:10
 */
@Repository
public class MaintainFeeAttachBizImpl extends BaseDao implements MaintainFeeAttachBiz {

  @Override
  public JsonResult list(PageResult2<T> page, MaintainFeeAttach maintainFeeAttach, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
    StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.name");
    sql.append(",m.standard,m.box_type,m.price,m.type,m.at_modules,m.remark,m.pinyin");
    sql.append(",m.longitude,m.latitude,m.radius,m.yard_dock");
    sql.append(",m.create_time,m.create_user,m.update_time,m.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_maintain_fee_attach m ");
    sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
    sql.append(" inner join sys_organization sognz on sognz.organization_id=m.organization_id ");
    sql.append(" where m.deleted=0 and sognz.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and m.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(maintainFeeAttach)) {
      if (Utils.isNotEmpty(maintainFeeAttach.getName())) {
        sql.append(" and (instr(m.name,:name)>0 or instr(m.pinyin,:name)>0) ");
        map.put("name", maintainFeeAttach.getName());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getStandard())) {
        sql.append(" and instr(m.standard,:standard)>0 ");
        map.put("standard", maintainFeeAttach.getStandard());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getYardDock())) {
        sql.append(" and instr(m.yard_dock,:yard_dock)>0 ");
        map.put("yard_dock", maintainFeeAttach.getYardDock());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getBoxType())) {
        sql.append(" and instr(m.box_type,:box_type)>0 ");
        map.put("box_type", maintainFeeAttach.getBoxType());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getType())) {
        sql.append(" and instr(m.type,:type)>0 ");
        map.put("type", maintainFeeAttach.getType());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getSort()) && Utils.isNotEmpty(maintainFeeAttach.getOrder())) {
        sql.append(" order by " + maintainFeeAttach.getSort() + " " + maintainFeeAttach.getOrder());
      }
    }
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<MaintainFeeAttach> list(MaintainFeeAttach maintainFeeAttach, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
    StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.name");
    sql.append(",m.standard,m.box_type,m.price,m.type,m.at_modules,m.remark,m.pinyin");
    sql.append(",m.longitude,m.latitude,m.radius,m.yard_dock");
    sql.append(",m.create_time,m.create_user,m.update_time,m.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_maintain_fee_attach m ");
    sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
    sql.append(" inner join sys_organization sognz on sognz.organization_id=m.organization_id ");
    sql.append(" where m.deleted=0 and sognz.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and m.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(maintainFeeAttach)) {
      if (Utils.isNotEmpty(maintainFeeAttach.getName())) {
        sql.append(" and (instr(m.name,:name)>0 or instr(m.pinyin,:name)>0) ");
        map.put("name", maintainFeeAttach.getName());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getStandard())) {
        sql.append(" and instr(m.standard,:standard)>0 ");
        map.put("standard", maintainFeeAttach.getStandard());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getYardDock())) {
        sql.append(" and instr(m.yard_dock,:yard_dock)>0 ");
        map.put("yard_dock", maintainFeeAttach.getYardDock());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getBoxType())) {
        sql.append(" and instr(m.box_type,:box_type)>0 ");
        map.put("box_type", maintainFeeAttach.getBoxType());
      }
      if (Utils.isNotEmpty(maintainFeeAttach.getType())) {
        sql.append(" and instr(m.type,:type)>0 ");
        map.put("type", maintainFeeAttach.getType());
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public MaintainFeeAttach getById(MaintainFeeAttach maintainFeeAttach) {
    RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select m.id,m.organization_id,m.name");
    sql.append(",m.standard,m.box_type,m.price,m.type,m.at_modules,m.remark");
    sql.append(",m.longitude,m.latitude,m.radius,m.yard_dock");
    sql.append(",m.create_time,m.create_user,m.update_time,m.update_user");
    sql.append(",sognz.organization_name organizationName,suser.username createName ");
    sql.append(" from chf_maintain_fee_attach m ");
    sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
    sql.append(" inner join sys_organization sognz on sognz.organization_id=m.organization_id ");
    sql.append(" where m.deleted=0 and sognz.deleted=0 and m.id=:id ");
    map.put("id", maintainFeeAttach.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean save(MaintainFeeAttach maintainFeeAttach, User loginUser) {
    maintainFeeAttach.setPinyin(PinyinUtils.ToFirstChar(maintainFeeAttach.getName() + maintainFeeAttach.getStandard()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into chf_maintain_fee_attach (");
    sql.append("organization_id,name,standard,box_type,type,at_modules");
    sql.append(",longitude,latitude,radius,yard_dock");
    sql.append(",pinyin,price,remark,create_user,create_time) values ( ");
    sql.append(":organization_id,:name,:standard,:box_type,:type,:at_modules");
    sql.append(",:longitude,:latitude,:radius,:yard_dock");
    sql.append(",:pinyin,:price,:remark,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("name", maintainFeeAttach.getName());
    map.put("standard", maintainFeeAttach.getStandard());
    map.put("box_type", maintainFeeAttach.getBoxType());
    map.put("pinyin", maintainFeeAttach.getPinyin());
    map.put("longitude", maintainFeeAttach.getLongitude());
    map.put("latitude", maintainFeeAttach.getLatitude());
    map.put("radius", maintainFeeAttach.getRadius());
    map.put("type", maintainFeeAttach.getType());
    map.put("at_modules", maintainFeeAttach.getAtModules());
    map.put("price", maintainFeeAttach.getPrice());
    map.put("yard_dock", maintainFeeAttach.getYardDock());
    map.put("remark", maintainFeeAttach.getRemark());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(MaintainFeeAttach maintainFeeAttach, User loginUser) {
    maintainFeeAttach.setPinyin(PinyinUtils.ToFirstChar(maintainFeeAttach.getName() + maintainFeeAttach.getStandard()));
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update chf_maintain_fee_attach set ");
    sql.append("name=:name,standard=:standard,box_type=:box_type,at_modules=:at_modules ");
    sql.append(",longitude=:longitude,latitude=:latitude,radius=:radius");
    sql.append(",yard_dock=:yard_dock,pinyin=:pinyin ");
    sql.append(",price=:price,remark=:remark,update_user=:update_user where id=:id ");
    map.put("name", maintainFeeAttach.getName());
    map.put("standard", maintainFeeAttach.getStandard());
    map.put("box_type", maintainFeeAttach.getBoxType());
    map.put("at_modules", maintainFeeAttach.getAtModules());
    map.put("pinyin", maintainFeeAttach.getPinyin());
    map.put("longitude", maintainFeeAttach.getLongitude());
    map.put("latitude", maintainFeeAttach.getLatitude());
    map.put("radius", maintainFeeAttach.getRadius());
    map.put("yard_dock", maintainFeeAttach.getYardDock());
    map.put("price", maintainFeeAttach.getPrice());
    map.put("remark", maintainFeeAttach.getRemark());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", maintainFeeAttach.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(MaintainFeeAttach maintainFeeAttach, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (maintainFeeAttach.getId().length() > 0) {
      for (String ID : maintainFeeAttach.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update chf_maintain_fee_attach set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}
